//      vfs.c
//      
//      Copyright 2011 DJ Shaji <djshaji@gmail.com>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.


#include "vfs.h"

void _vfs_explore_volumes ( GtkTreeStore * store, GList * list )
{
	gint i = 0, len = g_list_length ( list ) ;
	for ( i = 0 ; i < len ; i ++ )
	{
		GVolume * v = g_list_nth_data ( list, i ) ;
		gchar * name = g_volume_get_name ( v ) ;
		
		GMount * m = g_volume_get_mount ( v ) ;
		GFile * root = g_mount_get_root ( m ) ;
		
		gchar * path = g_file_get_path ( root ) ;
		GdkPixbuf * pixbuf = panel_menu_find_icon ( "gtk-cdrom", 32 ) ;
			
		GtkTreeIter iter ;
		gtk_list_store_append ( store, & iter ) ;
		
		gtk_list_store_set ( store, & iter, 0, pixbuf, 1, name, 2, path, -1 ) ;
		
		g_object_unref ( pixbuf ) ;
		g_free ( path ) ;
		
		g_object_unref ( root ) ;
		g_object_unref ( m ) ;
		
	}
	
}

void _vfs_explore_drives ( GtkTreeStore * store, GList * list )
{
	warn ( __func__ ) ;
	gint i = 0, len = g_list_length ( list ) ;
	warn ( "%d drives found", len ) ;
	
	for ( i = 0 ; i < len ; i ++ )
	{
		GVolume * v = g_list_nth_data ( list, i ) ;
		
		if ( g_drive_has_volumes ( v ))
		{
			GList * l = g_drive_get_volumes ( v ) ;
			_vfs_explore_volumes ( store, l ) ;
			
			g_list_foreach ( l, g_object_unref, null ) ;
			g_list_free ( l ) ;
		}
		/*
		else
		{
			gchar * name = g_drive_get_name ( v ) ;
			GFile * root = g_mount_get_root ( m ) ;
			
			gchar * path = g_file_get_path ( root ) ;
			GdkPixbuf * pixbuf = panel_menu_find_icon ( "gtk-cdrom", FM_ICON_SIZE_DEFAULT ) ;
				
			GtkTreeIter iter ;
			gtk_list_store_append ( store, & iter ) ;
			
			gtk_list_store_set ( store, & iter, 0, pixbuf, 1, name, 2, path, -1 ) ;
			
			g_object_unref ( pixbuf ) ;
			g_free ( path ) ;
			
			g_object_unref ( root ) ;
			g_object_unref ( m ) ;
			
			g_free ( m ) ;
		}
		*/
	}
	
}

void _vfs_explore_mounts ( GtkTreeModel * store, GList * list )
{
	gint i = 0, len = g_list_length ( list ) ;
	g_debug ( "%d mounts", len ) ;
	
	for ( i = 0 ; i < len ; i ++ )
	{
		gchar * mount = g_list_nth_data ( list, i ) ;
		gchar * name = g_mount_get_name ( mount ) ;
		
		GFile * f = g_mount_get_root ( mount ) ;
		gchar * path = g_file_get_path ( f ) ;
		
		GdkPixbuf * pixbuf = panel_menu_find_icon ( "gtk-harddisk", 32 ) ;
				
		GtkTreeIter iter ;
		gtk_list_store_append ( store, & iter ) ;
		
		gtk_list_store_set ( store, & iter, 0, pixbuf, 1, name, 2, path, -1 ) ;
		
		g_object_unref ( pixbuf ) ;
		g_free ( path ) ;
		
		g_object_unref ( f ) ;
		g_free ( name ) ;
	}
	
}

void _vfs_explore_unix_mounts ( GtkTreeStore * store )
{
	GList * list = g_unix_mount_points_get ( null ) ;
	gint i = 0, len = g_list_length ( list ) ;
	
	gchar drive_letter [] = { "C:" } ;
	
	for ( i = 0 ; i < len ; i ++ )
	{
		gchar * u = g_list_nth_data ( list, i ) ;
		gchar * name = g_unix_mount_point_guess_name ( u ) ;
		
		gchar * path = g_unix_mount_point_get_mount_path ( u ) ;
		GdkPixbuf * pixbuf = panel_menu_find_icon ( "gtk-harddisk", 32 ) ;
				
		GtkTreeIter iter ;
		gtk_list_store_append ( store, & iter ) ;
		
		gtk_list_store_set ( store, & iter, 0, pixbuf, 1, drive_letter, 2, path, -1 ) ;
		g_object_unref ( pixbuf ) ;
		
		g_free ( name ) ;
		drive_letter [0] ++ ;
	}
	
	g_list_foreach ( list, g_unix_mount_free, null ) ;
	g_list_free ( list ) ;
}

void vfs_explore_computer ( Explorer * e )
{
	GVolumeMonitor * vm = g_volume_monitor_get () ;

	gtk_list_store_clear ( e -> store ) ;
	_vfs_explore_unix_mounts ( e -> store ) ;
	
	_vfs_explore_drives_others ( e -> store, vm ) ;
	g_object_unref ( vm ) ;
}

vodi _vfs_explore_drives_others ( GtkTreeModel * store, GVolumeMonitor * vm )
{
	GList * list = g_volume_monitor_get_connected_drives ( vm ) ;
	gint i = 0, len = g_list_length ( list ) ;
	
	gchar drive_letter [] = { "Z:" } ;
	
	for ( i = 0 ; i < len ; i ++ )
	{
		GDrive * d = g_list_nth_data ( list, i ) ;
		if ( g_drive_has_volumes ( d ))
		{
			GList * l = g_drive_get_volumes ( d ) ;
			_vfs_explore_volumes ( store, l ) ;
			
			g_list_foreach ( l, g_object_unref, null ) ;
			g_list_free ( l ) ;
		
			continue ;
		}
		
		gchar * name = g_drive_get_name ( d ) ;
		
		gchar * n = null ;
		if ( g_drive_can_eject ( d ))
			n = g_strdup_printf ( "%s.cdrom", name ) ;
		else if ( g_drive_is_media_removable ( d ))
			n = g_strdup_printf ( "%s.usb", name ) ;
		else
			n = g_strdup_printf ( "%s.disk", name ) ;
		
		GdkPixbuf * pixbuf = panel_menu_find_icon ( "gtk-cdrom", 32 ) ;
				
		GtkTreeIter iter ;
		gtk_list_store_append ( store, & iter ) ;
		
		gtk_list_store_set ( store, & iter, 0, pixbuf, 1, drive_letter, 2, n, -1 ) ;
		g_object_unref ( pixbuf ) ;
		
		g_free ( name ) ;
		g_free ( n ) ;
		
		drive_letter [0] -- ;
	}
	
	g_list_foreach ( list, g_object_unref, null ) ;
	g_list_free ( list ) ;
}
